package cn.huanghuai.reggie.controller;


import cn.huanghuai.reggie.common.R;
import cn.huanghuai.reggie.dto.SetmealDto;
import cn.huanghuai.reggie.entity.Category;
import cn.huanghuai.reggie.entity.Setmeal;
import cn.huanghuai.reggie.entity.SetmealDish;
import cn.huanghuai.reggie.service.CategoryService;
import cn.huanghuai.reggie.service.SetmealDishService;
import cn.huanghuai.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.LMUL;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 套餐信息分页查询，因为含有分类名称，所以用SetmealDto类型返回
     *  后续也用来接收和返回修改信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> SetmealList(int page, int pageSize){
        //创建分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();


        //创建条件过滤器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加排序条件
        lambdaQueryWrapper.orderByDesc(Setmeal::getCreateTime);
            //执行查询
        setmealService.page(pageInfo,lambdaQueryWrapper);

        //将查询的信息除records外全部拷贝到setmealDtoPage中,因为setmealDtoPage中的records需要带有分类名称的信息
        BeanUtils.copyProperties(pageInfo, setmealDtoPage,"records");

        //准备处理分类名称信息，并将处理好的信息拷贝到setmealDtos中
        List<Setmeal> setmeals = pageInfo.getRecords();
        //循环处理
        List<SetmealDto> setmealDtos = setmeals.stream().map((item) ->{

            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtos);
        return R.success(setmealDtoPage);
    }

    /**
     * 新增套餐
     *  1.保存套餐基础信息
     *  2.保存套餐对应菜品
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        //保存套餐基础信息，保存操作会自动补全都对象中的id属性
        setmealService.save(setmeal);

        //获取套餐id和菜品信息
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();

        //保存套餐中菜品信息
        SetmealDish setmealDish = new SetmealDish();
        for (int i = 0; i < setmealDishList.size(); i++) {
            setmealDish = setmealDishList.get(i);
            setmealDish.setSetmealId(id);
            log.info(setmealDish.toString());
            setmealDishService.save(setmealDish);
        }

        return R.success("新增套餐成功");
    }

    /**
     * 删除套餐信息以及套餐中所包含的菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteBySetmealId(Long ids){
        //删除套餐基本信息
        setmealService.removeById(ids);

        //删除套餐对应菜品信息
            //创建条件构造器
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加过滤条件
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, ids);
            //执行删除查询
        setmealDishService.remove(lambdaQueryWrapper);

        return R.success("删除菜品成功");
    }

    /**
     * 根据套餐查询套餐信息以及相关菜品信息
     *
     *  未完成
     * @param setmealId
     * @return
     */
    @GetMapping("/{setmealId}")
    public R<SetmealDto> findSetmealDaoBySetmealId(@PathVariable Long setmealId){
        //创建将要返回的对象
        SetmealDto setmealDto = new SetmealDto();
        //查询套餐基础信息
        Setmeal setmeal = setmealService.getById(setmealId);
        //将基础套餐信息拷贝到setmealDto对象中
        BeanUtils.copyProperties(setmeal, setmealDto);
        return R.success(setmealDto);
    }

    /**
     * 根据套餐id查询套餐对应菜品以及套餐基础信息
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> SetmealDtoList(Long categoryId, int status){
        //创建出套餐以及对应Dto数组
        List<SetmealDto> setmealDtos = new ArrayList<>();

        //查询套餐基础信息,创建条件过滤器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加查询条件
        lambdaQueryWrapper.eq(Setmeal::getCategoryId, categoryId);
        lambdaQueryWrapper.eq(Setmeal::getStatus,status);
            //添加排序
        lambdaQueryWrapper.orderByDesc(Setmeal::getCreateTime);
            //执行查询
        List<Setmeal> setmeals  =  setmealService.list(lambdaQueryWrapper);

        for (int i = 0; i < setmeals.size(); i++) {
            SetmealDto setmealDto = new SetmealDto();
            Setmeal setmeal = setmeals.get(i);
            BeanUtils.copyProperties(setmeal, setmealDto);
            Long catId = setmeal.getCategoryId();

            //创建条件过滤器： 查询套餐对应菜品
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                //添加过滤条件
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,catId);
                //执行查询
            setmealDto.setSetmealDishes(setmealDishService.list(setmealDishLambdaQueryWrapper));

            setmealDtos.add(setmealDto);
        }

        return R.success(setmealDtos);
    }

}
